package com.tianji.contract.bill.task;

import com.tianji.contract.bill.task.BillTask;
import com.tianji.contract.pojo.ParkBill;
import com.tianji.contract.service.ParkBillService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.concurrent.DelayQueue;

@RequiredArgsConstructor
@Component // 确保该类被 Spring 容器管理
public class BillConsumer implements Runnable {

    private final DelayQueue<BillTask> delayQueue;
    private final ParkBillService parkBillService;

    @Override
    public void run() {
        while (true) {
            try {
                // take() 会阻塞，直到有任务到期
                BillTask take = delayQueue.take();
                Long billId = take.getBillId();
                parkBillService.lambdaUpdate()
                        .eq(ParkBill::getId, billId)
                        .set(ParkBill::getStatus, "CLOSED")
                        .update();
                System.out.println("账单 " + billId + " 已关闭。");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt(); // 恢复中断状态
                break; // 线程终止
            }
        }
    }
}